package com.inossem.warehousestorage_inossem.activity.receive;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.receive.extra.ReceiveExtra;
import com.inossem.warehousestorage_inossem.adapter.receive.SelectReceiverListAdapter;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveReceiveItemBean;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveReceiverBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.receive.ReceiveSelectReceiverRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.ReceiveUtils;

import java.util.ArrayList;
import java.util.List;

public class ReceiveSelectReceiverActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView receiverList;

    private SelectReceiverListAdapter adapter;

    private String conditionStr;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_receive_select_receiver);
        initView(ReceiveSelectReceiverActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(ReceiveSelectReceiverActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.receiverListRefresh);
        receiverList = (PullableListView) activity.findViewById(R.id.receiverList);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_select_receiver_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_add_receiver_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        receiverList.setPullUpFlag(false);
        receiverList.setPullDownFlag(false);
        getExtra(activity);
        getData(activity);
    }

    private void getExtra(Activity activity) {
        conditionStr = activity.getIntent().getStringExtra(ReceiveExtra.RECEIVER_CONDITION);
    }

    private void getData(Context context) {
        ReceiveSelectReceiverRequest.request(context, conditionStr, new GsonBeanListener<ReceiveReceiverBean>() {
            @Override
            public void onSuccess(ReceiveReceiverBean bean) {
                List<ReceiveReceiverBean.DataBean.ReceiverListBean> allReceiverList = bean.getData().getReceiverList();
                List<ReceiveReceiveItemBean.DataBean.ReceiverListBean> currentReceiverList = ReceiveUtils.getReceiveReceiveItemBean(context).getData().getReceiverList();
                List<ReceiveReceiverBean.DataBean.ReceiverListBean> resultReceiverList = filterReceiver(allReceiverList, currentReceiverList);
                addReceiverListData(context, resultReceiverList, receiverList);
            }
        });
    }

    private List<ReceiveReceiverBean.DataBean.ReceiverListBean> filterReceiver(List<ReceiveReceiverBean.DataBean.ReceiverListBean> allReceiverList, List<ReceiveReceiveItemBean.DataBean.ReceiverListBean> currentReceiverList) {
        List<ReceiveReceiverBean.DataBean.ReceiverListBean> removeReceiverList = new ArrayList<>();
        for (int i = 0; i < allReceiverList.size(); i++) {
            ReceiveReceiverBean.DataBean.ReceiverListBean receiverItem = allReceiverList.get(i);
            for (int j = 0; j < currentReceiverList.size(); j++) {
                if (receiverItem.getId().equals(currentReceiverList.get(j).getId())) {
                    removeReceiverList.add(receiverItem);
                }
            }
        }
        allReceiverList.removeAll(removeReceiverList);
        return allReceiverList;
    }


    private void addReceiverListData(Context context, List<ReceiveReceiverBean.DataBean.ReceiverListBean> selectReceiverList, ListView listView) {
        adapter = new SelectReceiverListAdapter(context, selectReceiverList, () -> {
            ReceiveSelectReceiverActivity.this.finish();
        });
        receiverList.setAdapter(adapter);
    }

    public interface ReceiveSelectReceiverListener {
        void onSelectReceiver();
    }

}
